#include "collectingbox.h"
#include <QDebug>
#include <QMessageBox>
#include <QApplication>
#include <QtEndian>
CollectingBox::CollectingBox(QObject *parent) : QObject(parent)
{
    mThread.start();
    this->moveToThread(&mThread);
    connect(this,SIGNAL(ToCreateDevice()),this,SLOT(Slot_CreateDevice()));
    emit ToCreateDevice();
    SlotInit();
}

CollectingBox::~CollectingBox()
{
    mThread.quit();
    mThread.wait();
}

void CollectingBox::ReadVoltageInformation()
{
    hasRecv=false;
    emit ToReadFunction(0x0002,1);
}

float CollectingBox::GetRecvVoltageValue()
{
    while(!hasRecv)
    {
        QApplication::processEvents();
    }
    return boxVoltage;
}

void CollectingBox::OpenDevice()
{
    emit ToOpenDevice();
}

void CollectingBox::CloseDevice()
{
    emit ToCloseDevice();
}

void CollectingBox::SlotInit()
{
    connect(this,SIGNAL(ToOpenDevice()),this,SLOT(Slot_OpenDevice()));
    connect(this,SIGNAL(ToReadFunction(ushort,ushort)),this,SLOT(Slot_ReadVoltage(ushort,ushort)));
    connect(this,SIGNAL(ToCloseDevice()),this,SLOT(Slot_CloseDevice()));
}

void CollectingBox::Slot_CreateDevice()
{
    modBusDevice=new Modbus();
    qDebug()<<"欢迎来此类旅行";
}

void CollectingBox::Slot_CloseDevice()
{
    qDebug()<<__FUNCTION__;
    modBusDevice->port.close();

    if(modBusDevice->port.isOpen())
    {
       QString text="串口关闭失败";
       emit ShowOpenComWidget(text);
    }
    else
    {
        QString text="串口关闭成功";
        emit ShowOpenComWidget(text);
    }
}

void CollectingBox::Slot_OpenDevice()
{
    modBusDevice->port.open(QSerialPort::ReadWrite);
    if(modBusDevice->port.isOpen())
    {
       QString text="串口打开成功";
       emit ShowOpenComWidget(text);
    }
    else
    {
        QString text="串口打开失败";
        emit ShowOpenComWidget(text);
    }
}

void CollectingBox::Slot_ReadVoltage(ushort start, ushort number)
{
    ushort recvRange=0;
    ushort recv[2]={0};
    unsigned char buf[4];
    memcpy(buf,recv,4);
    //先读量程
    bool ret=modBusDevice->SendFc3(slaveID,0x0010,1,&recvRange);
    if(ret==false)
    {
        boxVoltage=-1;
        hasRecv=true;
        return;
    }
    qDebug()<<"采集板读出来的量程为:"<<recvRange;

    ret=modBusDevice->SendFc3(slaveID,start,number,recv);
    if(ret==false)
    {
        boxVoltage=-1;
        hasRecv=true;
        return;
    }
    qDebug()<<recv[0];
    memcpy(buf,recv,2);
    short voltageInit=(buf[1]<<8)|(buf[0]);
    qDebug()<<"大端模式"<<qToBigEndian(recv[0]);
    qDebug()<<"小端模式"<<qToLittleEndian(recv[0]);
    qDebug()<<"采集板读出来的电压原始值为"<<voltageInit;
    if(recvRange>3000)
    {
        boxVoltage=(float)voltageInit;
    }
    else if(recvRange<=3000&&recvRange>300)
    {
        boxVoltage=(float)voltageInit/10;
    }
    else if(recvRange<=300&&recvRange>30)
    {
        boxVoltage=(float)voltageInit/100;
    }
    else if(recvRange<=30&&recvRange>3)
    {
        boxVoltage=(float)voltageInit/1000;
    }
    else if(recvRange<=3&&recvRange>0)
    {
        boxVoltage=(float)voltageInit/10000;
    }

    hasRecv=true;
}
